// Fixed By ups2000ups

const int BOXES = 9;

const int16 [] boxes_fh = {442, 443, 444, 445, 446, 447, 448, 449, 450};

void finishStage(Map@ map, bool portal){
	map.showEffect("quest/party/clear");
	map.playSound("Party1/Clear");
	map.showObject("gate");
	if(portal)
	   map.enablePortal("next00");
}

void wrong(Map@ map){
	map.showEffect("quest/party/wrong_kor");
	map.playSound("Party1/Failed");
}

void giveExp(Players@ players, int exp){
	for(int i=0; i<players.getAmount(); i++)
		if(players[i].getHP() > 0)
			players[i].giveExp(exp);
}

void cleared(NPC@ npc){
	npc.addText("Congratulations on clearing the quests for this stage. Please use the portal you see over there and move on to the next stage.");
	npc.sendNext();
	npc.end();
}

int getAmountInMap(Players@ players, Map@ map){
	int count = 0;
	for(int i=0; i<players.getAmount(); i++)
		if(players[i].getMap().getID() == map.getID() && players[i].getChannel().getID() == map.getChannel().getID())
			count++;
	return count;
}

int getPlayersOnFoothold(Players@ players, int16 fh){
	int count = 0;
	for(int i=0; i<players.getAmount(); i++)
		if(players[i].getFoothold() == fh)
			count++;
	return count;
}
int getPlayersOnFoothold(Players@ players, int16 min, int16 max){
	int count = 0;
	for(int i=0; i<players.getAmount(); i++)
		if(players[i].getFoothold() >= min && players[i].getFoothold() <= max)
			count++;
	return count;
}

void npc(Player @player, NPC @npc) {
	int state = npc.getState();
	Party@ party = player.getParty();
	Event@ event = player.getMap().getEvent();
	if(@party == null || @event == null){
		npc.end();
		return;
	}
	int inpq=0,inmap=0;
	if(party.getLeader() == player.getID()){
		inpq = getAmountInMap(party.getMembers(), player.getMap());
		inmap = event.getPlayers().getAmount();
	}
	int map = player.getMap().getID();
	if(party.getLeader() == player.getID()){
		if(party.getVariable("lpq_8s_first") == -1){
			if(inpq != inmap){
				npc.addText("I don't think every single member of your party is present. You'll need to bring each and every member from the previous stage here in order to take on this stage. Please find your missing party member(s).");
				npc.end();
			}
			else{
				npc.addText("Welcome to the 8th stage. Here you'll see a number of platforms to step on. #b5#k of those are connected to the #bportal that heads to the next stage#k. To beat it, place #b5 of your party members on the correct platform#k.\r A warning: You will have to be standing solid on the center of the platform in order to be counted as a correct answer. Also remember that only 5 members can be on the platform. Once that takes places, the leader of the party should #bdouble-click me to see if the answer's correct or not#k. Good luck!");
				party.setVariable("lpq_8s_first", 1);
				int [] boxes = {0, 0, 0, 0, 0, 0, 0, 0, 0};
				for(int i=0; i<5;){ // number of boxes
					int r = random(BOXES);
					if(boxes[r] == 0){
						boxes[r] = 1;
						i++;
					}
				}
				for(int i=0; i<BOXES; i++){
					party.setVariable("lpq_8s_boxes" + i, boxes[i]);
				}
			}
			npc.sendNext();
		}
		else if(party.getVariable("lpq_8s_done") == 1){
			cleared(npc);
			return;
		}
		else{
			int [] boxes = {0, 0, 0, 0, 0, 0, 0, 0, 0};
			for(int i=0; i<BOXES; i++){
				boxes[i] = getPlayersOnFoothold(player.getMap().getPlayers(), boxes_fh[i]);
			}
			int onboxes = 0;
			for(int i=0; i<BOXES; i++){
				onboxes += boxes[i];
			}
			if(onboxes != 5){ // number of boxes
				npc.addText("It looks like you haven't found the 5 boxes just yet. Please think of a different combination of boxes. And please make sure to remember that only 5 can be on the top of the boxes, stading firm on the center of them, in order to be counted as the right answer. Keep going!");
				npc.sendNext();
			}
			else{
				bool ans = true;
				for(int i=0; i<BOXES; i++){
					if(boxes[i] != party.getVariable("lpq_8s_boxes" + i)){
						ans = false;
						break;
					}
				}
				if(ans){
					finishStage(player.getMap(), true);
					giveExp(player.getMap().getPlayers(), 5040);
					party.setVariable("lpq_8s_done", 1);
				}
				else{
					wrong(player.getMap());
				}
			}
		}
	}
	else{
		npc.addText("Here's the information on the 8th stage. Here you'll see a number of platforms to step on. #b5#k of those are connected to the #bportal that heads to the next stage#k. To beat it, place #b5 of your party members on the correct platform#k.\r A warning: You will have to be standing solid on the center of the platform in order to be counted as a correct answer. Also remember that only 5 members can be on the platform. Once that takes places, the leader of the party should #bdouble-click me to see if the answer's correct or not#k. Good luck!");
		npc.sendNext();
	}
	npc.end();
}